package com.ruoyi.app.util;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.enums.RspEnmu;
import lombok.ToString;
import org.springframework.util.Assert;

@ToString(callSuper = true)
public class RspMsg extends BaseMsg {

    private String code;
    private String msg;

    public RspMsg(String code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public RspMsg(RspEnmu ok, Object data) {
        this.code = String.valueOf(ok.getCode());
        this.data = data;
    }

    public RspMsg(RspEnmu ok) {
        this.code = String.valueOf(ok.getCode());
    }

    public RspMsg(RspEnmu ok, String msg) {
        this.code = String.valueOf(ok.getCode());
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public RspMsg() {
        setRspEnmu(RspEnmu.OK);
    }

    public static RspMsg RspMsgOk() {
        RspMsg rspMsg = new RspMsg();
        rspMsg.setRspEnmu(RspEnmu.OK);
        return rspMsg;
    }

    public static RspMsg RspMsgOk(Object data) {
        RspMsg rspMsg = new RspMsg();
        rspMsg.setRspEnmu(RspEnmu.OK);
        rspMsg.setData(data);
        return rspMsg;
    }

    public static RspMsg ok(Object data) {
        return new RspMsg(RspEnmu.OK, data);
    }

    public static RspMsg ok() {
        return new RspMsg(RspEnmu.OK);
    }

    public static RspMsg ok(String msg) {
        return new RspMsg(RspEnmu.OK, msg);
    }

    public boolean isOk() {
        return "000000".equals(this.code);
    }

    public RspMsg ok(ReqMsg req, Object data) {
        return new RestResponseBuilder()
                .code(String.valueOf(RspEnmu.OK.getCode()))
                .msg(RspEnmu.OK.getMessage())
                .data(data)
                .build();
    }

    public RspMsg fail(ReqMsg req, RspEnmu rspEnmu) {
        return new RestResponseBuilder()
                .code(String.valueOf(rspEnmu.getCode()))
                .msg(rspEnmu.getMessage())
                .data(new JSONObject())
                .build();
    }

    public static RspMsg fail(RspEnmu rspEnmu) {
        RspMsg rspMsg = new RspMsg();
        rspMsg.setCode(String.valueOf(rspEnmu.getCode()));
        rspMsg.setMsg(rspEnmu.getMessage());
        return rspMsg;
    }

    public void setRspEnmu(RspEnmu rspEnmu) {
        setCode(String.valueOf(rspEnmu.getCode()));
        setMsg(rspEnmu.getMessage());
    }

    public static class RestResponseBuilder {
        private String code;
        private String msg;
        private Object data;

        private RestResponseBuilder code(String code) {
            Assert.notNull(code, "return code cannot be null");
            this.code = code;
            return this;
        }

        public RestResponseBuilder msg(String msg) {
            this.msg = msg;
            return this;
        }

        public RestResponseBuilder data(Object data) {
            this.data = data;
            return this;
        }

        public RspMsg build() {
            return new RspMsg(code, msg, data);
        }
    }
}